#include <QtGui>
#include <QtCore>
#include <QtSql>
#include "absencedialog.h"
#include "explication.h"
#include "reparation.h"
extern QString g_pathabsolu;
class explic;
class repare;
absencedialog::absencedialog()
{
setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setWindowFlags(Qt::WindowStaysOnTopHint);
pushButton->hide();
connect(pushButton,SIGNAL(pressed()),this,SLOT(lancerenov()));
connect(pushButton_2,SIGNAL(pressed()),this,SLOT(lancecree()));
}

/*void absencedialog::lancerenov(){
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
query.exec("ALTER TABLE `Alarmes` CHANGE `Alarm_IDDos` `Alarm_IDDos` VARCHAR( 36 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL;");
QSqlQuery query1;
query1.exec("ALTER TABLE Alarmes ADD Alarm_utilisateur VARCHAR(30) COLLATE utf8_unicode_ci NULL AFTER Alarm_valid;");
QMessageBox::information(0,QObject::tr("info"),QObject::trUtf8("La table Alarmes a été mise à jour"),QMessageBox::Ok);
repare *reparation = new repare;
reparation->show();

}*/

void absencedialog::lancecree(){
/*explic *explication = new explic;
explication->show();*/
//la requete se trouve dans la fonction void explic::fonctioncreetable() de explication.cpp
QSqlDatabase db = QSqlDatabase::database();
QString requete;
requete = "CREATE TABLE Alarmes (ID_Primkey BIGINT(20) NOT NULL AUTO_INCREMENT,"
			    	  "TextAlarme LONGTEXT COLLATE utf8_unicode_ci NULL,"
			          "Alarm_Date DATETIME NULL,"
			          "Alarm_AlarmDate DATETIME NULL,"
				  "Nom VARCHAR(50) COLLATE utf8_unicode_ci NULL,"
				  "Prenom VARCHAR(50) COLLATE utf8_unicode_ci NULL,"
			          "Alarm_IDDos VARCHAR(36) NULL,"
  				  "Alarm_valid BOOL NULL,"
				  "Alarm_utilisateur VARCHAR(30) COLLATE utf8_unicode_ci NULL,"
				  "PRIMARY KEY(ID_Primkey));";
QSqlQuery query(db);
query.exec(requete);
if(db.commit() == true){
    db.close();
    if(connexion()== false)
    {
        label->setText(trUtf8("Vérification impossible."));
        }
}
else{
    label->setText(trUtf8("Problème de fermeture de base."));
    }
}

bool absencedialog::connexion() {
QStringList liste;
QFile file(""+g_pathabsolu+"/utilisateur.ini");
    if(!file.open(QIODevice::ReadWrite | QIODevice::Text)){
        QMessageBox::warning(0,tr("erreur"),trUtf8("le fichier utilisateur.ini ne peut pas être ouvert."),QMessageBox::Ok);}
    QTextStream stream(&file);
    while(!stream.atEnd()){
        QString line = stream.readLine();
        liste << ""+line+"";}

QString pmotdepassechoix = liste[2];
QString putil            = liste[1];
QString phost            = liste[0];

QStringList motdepasslist = pmotdepassechoix.split("=");
QStringList utillist      = putil.split("=");
QStringList hostlist      = phost.split("=");

QString mot  = motdepasslist[1];
mot.replace(" ","");
QString util = utillist[1];
util.replace(" ","");
QString host = hostlist[1];
host.replace(" ","");

QSqlDatabase db2   = QSqlDatabase :: addDatabase("QMYSQL");
db2.setHostName    (""+host+"");
db2.setDatabaseName("DrTuxTest");
db2.setUserName    (""+util+"");
db2.setPassword    (""+mot+"");

if (!db2.open()) {
	QMessageBox::critical(0,"Erreur ouverture de base 104", db2.lastError().text());
	return false;
}
QStringList listetables;
listetables = db2.tables(QSql::Tables);

if(listetables.contains("Alarmes",Qt::CaseInsensitive)){
	label->setText(trUtf8("La table Alarmes a été installée,\nle widget se ferme dans 3 secondes."));
	QTimer::singleShot(3000,this,SLOT(close()));
		}
	else{

	label->setText(trUtf8("La table Alarmes n'a pas pu être installée."));

	QTimer::singleShot(5000,this,SLOT(close()));
		}

return true;
}
